{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f2fe3480",
   "metadata": {},
   "source": [
    "# 第11周\n",
    "* author:邱星倩\n",
    "* time：第11周周二上午\n",
    "* link: [语音识别](https://ai.baidu.com/tech/speech)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a844f09",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "## 1.知识概念  \n",
    "> 1.一切都是I/O：软工到产品的一般化知识    \n",
    "> 2.语音识别:speech recognition  \n",
    "> 3.语音唤醒  \n",
    "> 4.自动语音识别:Automatic Speech Recognition, 简称 ASR    \n",
    ">> a.其目标是以电脑自动将人类的语音内容转换为相应的文字。  \n",
    ">> b.与说话人识别及说话人确认不同，后者尝试识别或确认发出语音的说话人而非其中所包含的词汇内容。      \n",
    " \n",
    "> 5.语音合成:text to speech,简称 TTS      \n",
    ">> a.将文字转化为语音的一种技术，类似于人类的嘴巴，通过不同的音色说出想表达的内容。    \n",
    ">> b.在语音合成技术中，主要分为 语言分析部分 和 声学系统部分 ，也称为 前端部分 和 后端部分， 语言分析部分主要是根据输入的文字信息进行分析，生成对应的语言学规格书，想好该怎么读；    \n",
    ">> c.声学系统部分主要是根据语音分析部分提供的语音学规格书，生成对应的音频，实现发声的功能。    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce4dbc08",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6abf16d",
   "metadata": {},
   "source": [
    "# 2.语音识别测试"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8443ef5d",
   "metadata": {},
   "source": [
    "### 步骤\n",
    "> 1. 获取access_token  \n",
    "> 2. 准备请求参数（数据），俺指定的数据格式  \n",
    "> 3. 发起请求  \n",
    "> 4. 响应  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a389c5eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"refresh_token\":\"25.4add03f8c40585a9a78631c77891a4db.315360000.1968073821.282335-25321929\",\"expires_in\":2592000,\"session_key\":\"9mzdDFecpOMlGvp6UiHa1mgvTVNVHK+o\\/Fc9QRy7dyflvYfkl1fOiEt\\/a5GcsxCY+4vY3w\\/RnTbs6LM37U4lhU87aZNeeg==\",\"access_token\":\"24.7b575efd5da3fd49651be52c5e44f021.2592000.1655305821.282335-25321929\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"e1ed3f626d6c55730255cc7b4ba642c1\"}\n",
      "\n",
      "{'refresh_token': '25.4add03f8c40585a9a78631c77891a4db.315360000.1968073821.282335-25321929', 'expires_in': 2592000, 'session_key': '9mzdDFecpOMlGvp6UiHa1mgvTVNVHK+o/Fc9QRy7dyflvYfkl1fOiEt/a5GcsxCY+4vY3w/RnTbs6LM37U4lhU87aZNeeg==', 'access_token': '24.7b575efd5da3fd49651be52c5e44f021.2592000.1655305821.282335-25321929', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': 'e1ed3f626d6c55730255cc7b4ba642c1'}\n",
      "Request time cost 1.115450\n",
      "{\"corpus_no\":\"7098351816333304809\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"北京科技馆。\"],\"sn\":\"474794797941652713821\"}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "from urllib.request import urlopen\n",
    "from urllib.request import Request\n",
    "from urllib.error import URLError\n",
    "from urllib.parse import urlencode\n",
    "timer = time.perf_counter\n",
    "\n",
    "\n",
    "API_KEY = 'OYDEhX1ETdUGqrU74FkOLiYh'\n",
    "SECRET_KEY = '54r27F28gYrzIOdxYuxkhOt2GgcDe6dY'\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = './audio/16k.m4a'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "# 获取access_token\n",
    "def fetch_token():\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "    print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "    print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "# 语音识别接口的调用\n",
    "\n",
    "token = fetch_token()\n",
    "speech_data = []\n",
    "# 打开目标语音文件\n",
    "with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "    speech_data = speech_file.read()\n",
    "length = len(speech_data)\n",
    "if length == 0:\n",
    "    raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "params = {\n",
    "    'cuid': CUID, \n",
    "    'token': token, \n",
    "    'dev_pid': DEV_PID\n",
    "}\n",
    "#测试自训练平台需要打开以下信息\n",
    "#params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "params_query = urlencode(params);\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "    'Content-Length': length\n",
    "}\n",
    "\n",
    "url = ASR_URL + \"?\" + params_query\n",
    "\n",
    "# print post_data\n",
    "req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "# 异常处理，避免出现红色的bug（提前预判可能出现的异常）\n",
    "try:\n",
    "    begin = timer()\n",
    "    f = urlopen(req)\n",
    "    result_str = f.read()\n",
    "    print(\"Request time cost %f\" % (timer() - begin))\n",
    "except  URLError as err:\n",
    "    print('asr http response http code : ' + str(err.code))\n",
    "    result_str = err.read()\n",
    "\n",
    "if (IS_PY3):\n",
    "    result_str = str(result_str, 'utf-8')\n",
    "print(result_str)\n",
    "with open(\"result.txt\", \"w\") as of:\n",
    "    of.write(result_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56ea01f4",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "958ba573",
   "metadata": {},
   "source": [
    "# 3.语音合成接口测试"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5876f90d",
   "metadata": {},
   "source": [
    "# 4 真对话机器人（文本回复）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7e025c1",
   "metadata": {},
   "source": [
    "* 1.识别语音-->转换百度语音识别指定格式文件\n",
    "* 2.调用百度asr接口，转换成文本\n",
    "* 3.回复？数据结构：字典 {key:value} , key（人） 作为输入 Input, value（机器） 作为 Output\n",
    "> {\"你好呀\":\"你也好呀，看起来心情不错呀！\"}\n",
    "* 4.回复value的信息\n",
    "* 5.接语音合成，完成智能语音对话机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "589672d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你好呀\":\"你也好呀，看起来心情不错呀\",\n",
    "    \"你叫什么\":\"我是小度呀\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b9da995b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你也好呀，看起来心情不错呀'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa.get(\"你好呀\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "80775c17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我是小度呀'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa.get(\"你叫什么\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9a69f24",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb09c533",
   "metadata": {},
   "source": [
    "# 5.安装调用麦克风模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "84f2a676",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting SpeechRecognition\n",
      "  Downloading SpeechRecognition-3.8.1-py2.py3-none-any.whl (32.8 MB)\n",
      "Installing collected packages: SpeechRecognition\n",
      "Successfully installed SpeechRecognition-3.8.1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x000001B70877BB20>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/speechrecognition/\n",
      "WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x000001B70877BD30>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/speechrecognition/\n"
     ]
    }
   ],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d2936d0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pipwin\n",
      "  Downloading pipwin-0.5.2.tar.gz (7.9 kB)\n",
      "Collecting docopt\n",
      "  Downloading docopt-0.6.2.tar.gz (25 kB)\n",
      "Requirement already satisfied: requests in f:\\anaconda\\lib\\site-packages (from pipwin) (2.25.1)\n",
      "Collecting pyprind\n",
      "  Downloading PyPrind-2.11.3-py2.py3-none-any.whl (8.4 kB)\n",
      "Requirement already satisfied: six in f:\\anaconda\\lib\\site-packages (from pipwin) (1.15.0)\n",
      "Requirement already satisfied: beautifulsoup4>=4.9.0 in f:\\anaconda\\lib\\site-packages (from pipwin) (4.9.3)\n",
      "Collecting js2py\n",
      "  Downloading Js2Py-0.71-py3-none-any.whl (1.0 MB)\n",
      "Requirement already satisfied: packaging in f:\\anaconda\\lib\\site-packages (from pipwin) (20.9)\n",
      "Note: you may need to restart the kernel to use updated packages.Collecting pySmartDL>=1.3.1\n",
      "\n",
      "  Downloading pySmartDL-1.3.4-py3-none-any.whl (20 kB)\n",
      "Requirement already satisfied: soupsieve>1.2 in f:\\anaconda\\lib\\site-packages (from beautifulsoup4>=4.9.0->pipwin) (2.2.1)\n",
      "Collecting tzlocal>=1.2\n",
      "  Downloading tzlocal-4.2-py3-none-any.whl (19 kB)\n",
      "Collecting pyjsparser>=2.5.1\n",
      "  Downloading pyjsparser-2.7.1.tar.gz (24 kB)\n",
      "Collecting backports.zoneinfo\n",
      "  Downloading backports.zoneinfo-0.2.1-cp38-cp38-win_amd64.whl (38 kB)\n",
      "Collecting pytz-deprecation-shim\n",
      "  Downloading pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)\n",
      "Collecting tzdata\n",
      "  Downloading tzdata-2022.1-py2.py3-none-any.whl (339 kB)\n",
      "Requirement already satisfied: pyparsing>=2.0.2 in f:\\anaconda\\lib\\site-packages (from packaging->pipwin) (2.4.7)\n",
      "Requirement already satisfied: idna<3,>=2.5 in f:\\anaconda\\lib\\site-packages (from requests->pipwin) (2.10)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in f:\\anaconda\\lib\\site-packages (from requests->pipwin) (1.26.4)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in f:\\anaconda\\lib\\site-packages (from requests->pipwin) (2020.12.5)\n",
      "Requirement already satisfied: chardet<5,>=3.0.2 in f:\\anaconda\\lib\\site-packages (from requests->pipwin) (4.0.0)\n",
      "Building wheels for collected packages: pipwin, docopt, pyjsparser\n",
      "  Building wheel for pipwin (setup.py): started\n",
      "  Building wheel for pipwin (setup.py): finished with status 'done'\n",
      "  Created wheel for pipwin: filename=pipwin-0.5.2-py2.py3-none-any.whl size=8776 sha256=d8b28d32308f77c2e06abb00a02ad5c6ea8cd173b08b52bf087ffddb7f6c7503\n",
      "  Stored in directory: c:\\users\\thinkpad\\appdata\\local\\pip\\cache\\wheels\\f4\\a8\\76\\372879a40d2d4dd7de23efd867ed37112687429b8d0dda4545\n",
      "  Building wheel for docopt (setup.py): started\n",
      "  Building wheel for docopt (setup.py): finished with status 'done'\n",
      "  Created wheel for docopt: filename=docopt-0.6.2-py2.py3-none-any.whl size=13705 sha256=8306d6b823916a80142e57757b89e192a095c3c8ea5e6eed6054e9da6adb1915\n",
      "  Stored in directory: c:\\users\\thinkpad\\appdata\\local\\pip\\cache\\wheels\\56\\ea\\58\\ead137b087d9e326852a851351d1debf4ada529b6ac0ec4e8c\n",
      "  Building wheel for pyjsparser (setup.py): started\n",
      "  Building wheel for pyjsparser (setup.py): finished with status 'done'\n",
      "  Created wheel for pyjsparser: filename=pyjsparser-2.7.1-py3-none-any.whl size=25998 sha256=4529ad709a64dc56d767d6aaf53cab48464e175a8f44e58c8bf05967b3020684\n",
      "  Stored in directory: c:\\users\\thinkpad\\appdata\\local\\pip\\cache\\wheels\\d5\\88\\34\\ccb5bb40eb3178a134eb293e6c363928c5bcfba0b91031db76\n",
      "Successfully built pipwin docopt pyjsparser\n",
      "Installing collected packages: tzdata, backports.zoneinfo, pytz-deprecation-shim, tzlocal, pyjsparser, pySmartDL, pyprind, js2py, docopt, pipwin\n",
      "Successfully installed backports.zoneinfo-0.2.1 docopt-0.6.2 js2py-0.71 pipwin-0.5.2 pySmartDL-1.3.4 pyjsparser-2.7.1 pyprind-2.11.3 pytz-deprecation-shim-0.1.0.post0 tzdata-2022.1 tzlocal-4.2\n"
     ]
    }
   ],
   "source": [
    "pip install pipwin"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb2d535d",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b53ca05c",
   "metadata": {},
   "source": [
    "# 6.调用电脑麦克风，将语音转换音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "5ab30155",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyaudio import PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "0a18d48b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "录音开始\n",
      "录音结束\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "print('录音开始')\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "with open(\"123.wav\",\"wb\")as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "print('录音结束')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64de80de",
   "metadata": {},
   "source": [
    "### 语音转文字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "062b0f45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"corpus_no\":\"7112410077569280641\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"今晚吃什么？\"],\"sn\":\"222242905111655987016\"}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "from urllib.request import urlopen\n",
    "from urllib.request import Request\n",
    "from urllib.error import URLError\n",
    "from urllib.parse import urlencode\n",
    "timer = time.perf_counter\n",
    "\n",
    "\n",
    "API_KEY = 'OYDEhX1ETdUGqrU74FkOLiYh'\n",
    "SECRET_KEY = '54r27F28gYrzIOdxYuxkhOt2GgcDe6dY'\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = '123.wav'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "# 获取access_token\n",
    "def fetch_token():\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "\n",
    "    result = json.loads(result_str)\n",
    "\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "# 语音识别接口的调用\n",
    "\n",
    "token = fetch_token()\n",
    "speech_data = []\n",
    "# 打开目标语音文件\n",
    "with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "    speech_data = speech_file.read()\n",
    "length = len(speech_data)\n",
    "if length == 0:\n",
    "    raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "params = {\n",
    "    'cuid': CUID, \n",
    "    'token': token, \n",
    "    'dev_pid': DEV_PID\n",
    "}\n",
    "#测试自训练平台需要打开以下信息\n",
    "#params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "params_query = urlencode(params);\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "    'Content-Length': length\n",
    "}\n",
    "\n",
    "url = ASR_URL + \"?\" + params_query\n",
    "\n",
    "# print post_data\n",
    "req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "# 异常处理，避免出现红色的bug（提前预判可能出现的异常）\n",
    "try:\n",
    "    begin = timer()\n",
    "    f = urlopen(req)\n",
    "    result_str = f.read()\n",
    "\n",
    "except  URLError as err:\n",
    "\n",
    "    result_str = err.read()\n",
    "\n",
    "if (IS_PY3):\n",
    "    result_str = str(result_str, 'utf-8')\n",
    "print(result_str)\n",
    "with open(\"result.txt\", \"w\") as of:\n",
    "    of.write(result_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b4b4769",
   "metadata": {},
   "source": [
    "### 机器人文本回复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "5cdcf762",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"今晚吃什么？\":\"我也不知道\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "7595d386",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我也不知道\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "from urllib.request import urlopen\n",
    "from urllib.request import Request\n",
    "from urllib.error import URLError\n",
    "from urllib.parse import urlencode\n",
    "timer = time.perf_counter\n",
    "\n",
    "\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = '123.wav'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "\n",
    "# 极速版\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "\n",
    "\n",
    "# 获取access_token\n",
    "def fetch_token():\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "       \n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "#     print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "#     print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    token = fetch_token()\n",
    "    speech_data = []\n",
    "    with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "        speech_data = speech_file.read()\n",
    "    length = len(speech_data)\n",
    "    if length == 0:\n",
    "        raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "    params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID}\n",
    "    #测试自训练平台需要打开以下信息\n",
    "    #params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "    params_query = urlencode(params);\n",
    "\n",
    "    headers = {\n",
    "        'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "        'Content-Length': length\n",
    "    }\n",
    "\n",
    "    url = ASR_URL + \"?\" + params_query\n",
    "\n",
    "    # print post_data\n",
    "    req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "    try:\n",
    "        begin = timer()\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "        \n",
    "    except  URLError as err:\n",
    "        \n",
    "        result_str = err.read()\n",
    "\n",
    "    if (IS_PY3):\n",
    "        result_str = str(result_str, 'utf-8')\n",
    "#    print(eval(result_str)[\"result\"][0])\n",
    "#     print(qa)\n",
    "    print(qa.get(eval(result_str)[\"result\"][0]))\n",
    "    with open(\"result.txt\", \"w\") as of:\n",
    "        of.write(result_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea075bf6",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
